/*
{ * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.cco;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.cco.exception.CentroComercialException;
import co.gov.medellin.sdep.sisdep.cco.exception.SolicitudLocalException;
import co.gov.medellin.sdep.sisdep.cco.exception.SolicitudLocalRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.service.ICentroComercialService;
import co.gov.medellin.sdep.sisdep.cco.service.ISolicitudLocalService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.SolicitudLocal;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaDecFormVentaException;

/**
 * @author Usuario
 * 
 */
public class SolicitudLocalController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/** bean para el manejo del formulario de busqueda SolicitudLocal */
	private SolicitudLocal solicitudLocalSearch = new SolicitudLocal();

	/** Objeto contenedor del listado de SolicitudLocal luego de una consulta. */
	private transient DataModel solicitudLocalListModel;

	/** bean para el manejo del formulario de creacion/edicion. SolicitudLocal */
	private SolicitudLocal solicitudLocal = new SolicitudLocal();

	/** instancia de servicio solicitudLocalService */
	private ISolicitudLocalService solicitudLocalService;
	
	/** instancia de servicio centroComercialService */
	private ICentroComercialService centroComercialService;

	/** instancia de servicio feriaDecFormVentaService */
	private List listaCentrosComerciales;
	
	LocalEntregaController localEntregaController;
	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public SolicitudLocalController() {
		this.solicitudLocalListModel = new ListDataModel();
	}
	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchSolicitudLocalByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getSolicitudLocalService().load(1, 10,
					this.solicitudLocalSearch);
			this.solicitudLocalListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.solicitudLocalListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.solicitudLocalListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.SOLICITUD_LOCAL_SEARCH_VIEW;
	}
	

	/**
	 * 
	 * @return String
	 */
	public String searchSolicitudLocalByRadicado() {
		String msg = "";
		try {
			List valores = null;
			valores = getSolicitudLocalService().loadByRadicado(1, 10,
					this.solicitudLocalSearch);
			this.solicitudLocalListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.solicitudLocalListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.solicitudLocalListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.SOLICITUD_LOCAL_SEARCH_VIEW;
	}	

	/**
	 * 
	 * @return
	 */
	public String cancelSolicitudLocal() {
		return AppConstants.SOLICITUD_LOCAL_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String entregaLocal() {
		Map params = getExternalContext().getRequestParameterMap();
		String solicitudLocalDataIndex = (String) params
				.get("solicitudLocalDataIndex");
		this.solicitudLocal = (SolicitudLocal) ((List) this.solicitudLocalListModel
				.getWrappedData())
				.get(Integer.parseInt(solicitudLocalDataIndex));
		
		getLocalEntregaController().localEntregaSearch.setIdCentroComercial(solicitudLocal.getIdCentroComercial());
		getLocalEntregaController().localEntregaSearch.setIdSolicitud(solicitudLocal.getIdSolicitud());
		getLocalEntregaController().localEntregaSearch.setStrCentroComercial(solicitudLocal.getStrCentroComercial());
		getLocalEntregaController().findByCriteria();
		return AppConstants.LOCAL_ENTREGA_SEARCH_VIEW;
	}

  /**
	 * Metodo que permite espeficicar la funcionalidad para la gestion [Crear / Actualizar] de un elmento tipo:
	 * SolicitudLocal.
	 *     
	 * @author programador.java@consult-soft.com  05-06-2012
	 * @return String Vista a la cual debe retornar
	 * 	 
	 */
	public String manage() {
		String returnView = AppConstants.SOLICITUD_LOCAL_SEARCH_VIEW;
		if(this.solicitudLocal.getId() == null) {
       returnView = create();
		} else {
       returnView = update();
		}
		return returnView;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.solicitudLocal.getId() == null) {
				solicitudLocal.setIdCentroComercial(new Long(solicitudLocal.getIdCentroComercialStr()));
				solicitudLocal.setIdTipologia(new Long(solicitudLocal.getIdTipologiaStr()));
				getSolicitudLocalService().insert(this.solicitudLocal);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (SolicitudLocalException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (SolicitudLocalRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- solicitudLocal " + msg, null, null);
		return AppConstants.SOLICITUD_LOCAL_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.solicitudLocal.getId() != null) {
				solicitudLocal.setIdCentroComercial(new Long(solicitudLocal.getIdCentroComercialStr()));
				solicitudLocal.setIdTipologia(new Long(solicitudLocal.getIdTipologiaStr()));
				getSolicitudLocalService().update(this.solicitudLocal);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (SolicitudLocalException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (SolicitudLocalRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.SOLICITUD_LOCAL_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * @throws SolicitudLocalException 
	 * 
	 */
	public String createSolicitudLocal() throws SolicitudLocalException {
		solicitudLocal = new SolicitudLocal();
		solicitudLocal.setId(null);
		solicitudLocal.setUsuarioCrea(getLoggedUserID());
		solicitudLocal.setUsuarioModifica(getLoggedUserID());
	//	solicitudLocal.setEstado("ACTIVO");
		listaCentrosComerciales = new ArrayList();
		try {
			if(getCentroComercialService().listaCentrosComerciales() != null){
				for(int i = 0; i<getCentroComercialService().listaCentrosComerciales().size();i++){			
					List listaT = new ArrayList();
					String registro[];
					listaT = (List)getCentroComercialService().listaCentrosComerciales().get(i);	
					registro = (String[])listaT.get(0);
					listaCentrosComerciales.add(new SelectItem(registro[0].toString(),registro[1].toString()));
					listaT = null;
					registro=null;
				}	
			}
		} catch (CentroComercialException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return AppConstants.SOLICITUD_LOCAL_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 * @throws SolicitudLocalException 
	 */
	public String updateSolicitudLocal() throws SolicitudLocalException {
		Map params = getExternalContext().getRequestParameterMap();
		String solicitudLocalDataIndex = (String) params
				.get("solicitudLocalDataIndex");
		this.solicitudLocal = (SolicitudLocal) ((List) this.solicitudLocalListModel
				.getWrappedData())
				.get(Integer.parseInt(solicitudLocalDataIndex));
		listaCentrosComerciales = new ArrayList();
		try {
			if(getCentroComercialService().listaCentrosComerciales() != null){
				for(int i = 0; i<getCentroComercialService().listaCentrosComerciales().size();i++){			
					List listaT = new ArrayList();
					String registro[];
					listaT = (List)getCentroComercialService().listaCentrosComerciales().get(i);	
					registro = (String[])listaT.get(0);
					listaCentrosComerciales.add(new SelectItem(registro[0].toString(),registro[1].toString()));
					listaT = null;
					registro=null;
				}	
			}
		} catch (CentroComercialException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		return AppConstants.SOLICITUD_LOCAL_UPDATE_VIEW;
	}


	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public SolicitudLocal getSolicitudLocalSearch() {
		return solicitudLocalSearch;
	}

	/**
	 * 
	 * @param solicitudLocalSearch
	 */
	public void setsolicitudLocalSearch(SolicitudLocal solicitudLocalSearch) {
		this.solicitudLocalSearch = solicitudLocalSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getsolicitudLocalListModel() {
		return solicitudLocalListModel;
	}

	/**
	 * 
	 * @param solicitudLocalListModel
	 */
	public void setsolicitudLocalListModel(DataModel solicitudLocalListModel) {
		this.solicitudLocalListModel = solicitudLocalListModel;
	}

	/**
	 * 
	 * @return
	 */
	public SolicitudLocal getSolicitudLocal() {
		return solicitudLocal;
	}

	/**
	 * 
	 * @param solicitudLocal
	 */
	public void setsolicitudLocal(SolicitudLocal solicitudLocal) {
		this.solicitudLocal = solicitudLocal;
	}

	/**
	 * obtencion del servicio solicitudLocalService
	 * 
	 * @return
	 */
	public ISolicitudLocalService getSolicitudLocalService() {
		return solicitudLocalService;
	}

	/**
	 * establecimiento del servicio solicitudLocalService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param solicitudLocalService
	 */
	public void setSolicitudLocalService(
			ISolicitudLocalService solicitudLocalService) {
		this.solicitudLocalService = solicitudLocalService;
	}

	public List getListaCentrosComerciales() {
		return listaCentrosComerciales;
	}

	public void setListaCentrosComerciales(List listaCentrosComerciales) {
		this.listaCentrosComerciales = listaCentrosComerciales;
	}

	public ICentroComercialService getCentroComercialService() {
		return centroComercialService;
	}

	public void setCentroComercialService(
			ICentroComercialService centroComercialService) {
		this.centroComercialService = centroComercialService;
	}

	public LocalEntregaController getLocalEntregaController() {
		return localEntregaController;
	}

	public void setLocalEntregaController(
			LocalEntregaController localEntregaController) {
		this.localEntregaController = localEntregaController;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
